///###############
//FILE          : MyDate.java
//WRITER        : Lior Zimmerman, liorz06, 066462300
//WRITER        : Ohad Dan, ohad_dan, 036767960
//EXERCISE      : oop ex1 2008
//DESCRIPTION   : this is a wrapper class to the Date class of java.
//###############
package oop.ex1.util;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
/**
 * A date implementation, can be set in personal design format.
 */
public class MyDate {

    Date _date;
    SimpleDateFormat _formatter;
   /**
    * Constructs a new date instance with the given format.
    * @param date The date.
    * @param format The format in which the date is set.
    * @throws InvalidDateException In case of an illegal date insertion.
    */
    public MyDate( String date, String format ) throws InvalidDateException {
        try{
            _formatter = new SimpleDateFormat( format );
            _formatter.setLenient(false);
            _date = _formatter.parse(date);         
        }
        catch( ParseException e ){
            throw new InvalidDateException( "Invalid date entered!" );
        }
    }
    /**
     * Constructs a new date instance.
     * @param date The date.
     * @throws InvalidDateException In case of an illegal date insertion.
     */
    public MyDate( String date ) throws InvalidDateException {
        try{
            _formatter = new SimpleDateFormat( "dd/MM/yyyy" );
            _formatter.setLenient(false);
            _date = _formatter.parse(date);         
        }
        catch( ParseException e ){
            throw new InvalidDateException( "Invalid date entered!" );
        }
    }
    /**
     * Returns a string representation of the date.
     * @return A string representation of the date.
     */
    public String toString() {
        return _formatter.format(_date);
    }
}
